<?php

namespace FileUD;

/**
 * @class   abstract
 * @author  Jon
 * @package FileUD
 * @link    https://gitee.com/joncc/file
 */
abstract class File
{
    /**
     * @return bool
     */
    public static function hasFile()
    {
        if (empty($_FILES) || $_FILES["file"]["error"] > 0) {
            return false;
        }

        return true;
    }

    /**
     * @return mixed
     */
    public static function getFileName()
    {
        return $_FILES["file"]["name"];
    }

    /**
     * @return mixed
     */
    public static function getFileType()
    {
        return $_FILES["file"]["type"];
    }

    /**
     * @return mixed
     */
    public static function getFileSize()
    {
        return $_FILES["file"]["size"];
    }

    /**
     * @return mixed
     */
    public static function getFileTmpName()
    {
        return $_FILES["file"]["tmp_name"];
    }

    /**
     * @return string
     */
    public static function getTimestamp()
    {
        return date('YmdHis', time()) . '-' . explode(' ', microtime())[0] * 1000000;
    }

    /**
     * @param string $path
     * @param string $name
     * @return bool
     */
    public static function moveUploadedFile($path, $name = '')
    {
        if (empty($path)) {
            return false;
        }

        @mkdir($path, 0777, true);

        $destination = $path . DIRECTORY_SEPARATOR;
        if (empty($name)) {
            $destination .= self::getTimestamp() . '-' . self::getFileName();
        } else {
            $destination .= $name;
        }

        return move_uploaded_file(self::getFileTmpName(), $destination);
    }

    /**
     * @return string
     */
    abstract public static function getFileSuffix();
}